module("core");

test("basic", function(){
    expect(1);
    var worker = JsWorker.createWorkerFromUrl("workers/echo.js");
	alert("Worker used in this test ==> " + worker.getName());
	ok(worker, "The worker from url is created successfully.");
});

test("passing JSON object", function() {
	expect(1);
	var obj = {"test" : 123};
	var worker;
	function onmessage(msg) {
		equals(msg.data.test, obj.test, "JSON object can be marshalled/unmarshalled between main thread and worker thread.");
		start();
	}
	worker = JsWorker.createWorkerFromUrl("workers/echo.js", onmessage);
	worker.postMessage(obj);
	stop();
});

test("worker-echo", function(){
	expect(1);
	var text = "test";
	function onmessage(msg) {
		equals(msg.data, text);
		start();
	}
	var worker = JsWorker.createWorkerFromUrl("workers/echo.js", onmessage);
	worker.postMessage(text);
	stop();
});

test("worker-adder", function() {
	expect(1);
	var worker;
	function onmessage(msg) {
		equals(msg.data, 3);
		start();
	}
	worker = JsWorker.createWorkerFromUrl("workers/adder.js", onmessage);
	worker.postMessage({"a" : 1, "b" : 2});
	stop();
});

test("worker-fib", function() {
	expect(1);
	function onmessage(msg) {
		equals(msg.data, 8);
		start();
	}
	var worker = JsWorker.createWorkerFromUrl("workers/fib.js", onmessage);
	worker.postMessage(5);
	stop();
});

test("worker-error", function() {
	expect(1);
	function onerror(error) {
		ok(error, "We need this error!");
		start();
	}
	function onmessage(message) {
		alert(message);
	}
	var worker = JsWorker.createWorkerFromUrl("workers/error.js", null, onerror);
	worker.postMessage("test");
	stop();
});

